/**
 * Create by CaoWenjian
 *
 * @copyRight: 2018
 **/
package com.bjboot.caocao.common.controller;

import com.alibaba.fastjson.JSON;
import com.bjboot.caocao.common.service.GeneratorService;
import com.bjboot.caocao.common.utils.GenUtils;
import com.bjboot.caocao.common.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@Description 代码生成器对应的控制器
 *@Author 操文健
 *@Date 2018-09-03 下午7:51
 *@Version 1.0
 **/
@RequestMapping("/common/generator")
@Controller
public class GeneratorController {

    String prefix = "common/generator";

    @Autowired
    private GeneratorService generatorService;

    @ApiOperation(value = "文件生成Module")
    @GetMapping()
    String generator() {
        return prefix + "/list";
    }

    @ApiOperation(value = "查询所有数据库")
    @ResponseBody
    @GetMapping("/list")
    List<Map<String, Object>> list() {
        List<Map<String, Object>> list = generatorService.list();
        return list;
    };

    @ApiOperation(value = "根据表名生成对应的文件包")
    @RequestMapping("/code/{tableName}")
    public void code(HttpServletRequest request, HttpServletResponse response,
                     @PathVariable("tableName") String tableName) throws IOException {
        String[] tableNames = new String[] { tableName };
        byte[] data = generatorService.generatorCode(tableNames);
        // 设置response相应信息
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"bjBoot.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        // 将数据通过相应流写入到前端
        IOUtils.write(data, response.getOutputStream());
    }

    @ApiOperation(value = "批量生成对应的文件包")
    @RequestMapping("/batchCode")
    public void batchCode(HttpServletRequest request, HttpServletResponse response, String tables) throws IOException {
        String[] tableNames = new String[] {};
        tableNames = JSON.parseArray(tables).toArray(tableNames);
        byte[] data = generatorService.generatorCode(tableNames);
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"bootdo.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }

    @ApiOperation(value = "返回参数编辑界面Module")
    @GetMapping("/edit")
    public String edit(Model model) {
        Configuration conf = GenUtils.getConfig();
        Map<String, Object> property = new HashMap<>(16);
        property.put("author", conf.getProperty("author"));
        property.put("email", conf.getProperty("email"));
        property.put("package", conf.getProperty("package"));
        property.put("autoRemovePre", conf.getProperty("autoRemovePre"));
        property.put("tablePrefix", conf.getProperty("tablePrefix"));
        model.addAttribute("property", property);
        return prefix + "/edit";
    }

    @ResponseBody
    @PostMapping("/update")
    Result update(@RequestParam Map<String, Object> map) {
        // 不允许修改配置文件，后台限制死
        return Result.error("系统不允许修改");
        /*try {
            PropertiesConfiguration conf = new PropertiesConfiguration("generator.properties");
            conf.setProperty("author", map.get("author"));
            conf.setProperty("email", map.get("email"));
            conf.setProperty("package", map.get("package"));
            conf.setProperty("autoRemovePre", map.get("autoRemovePre"));
            conf.setProperty("tablePrefix", map.get("tablePrefix"));
            conf.save();
        } catch (ConfigurationException e) {
            return Result.error("保存配置文件出错");
        }
        return Result.ok();*/
    }

}
